home *** CD-ROM | disk | FTP | other *** search
- {A note from the author:
-
- I needed to do some spiffy things with the listboxes so I wrote this. If it
- already exists, then great, but I couldn't find it. With this small program,
- you can multi-select items from ListBox1 and drag them to ListBox2.
- No big deal, except that you can INSERT a selected item into a specific spot
- in the list.
-
- The really cool thing here is that you can select an item in ListBox2 and
- move it into a another spot within the list by using the arrows or by dragging
- and dropping. Again, I couldn't find any code that already did this. I hope
- you find this code useful, and if you do any other cool things with it, please
- let me know. This code is only lightly documented, so if you have any
- questions, jsut ask. One reason for the length of this program is that
- it is relatively (dare I say it?) bug-free. I'll probably pay for that claim.
-
- Richard Howard 71553,2544
- Mei Technology Corporation
- 26 August 1995}
-
- unit CoolBox;
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls, Buttons;
-
- type
- TForm1 = class(TForm)
- ListBox1: TListBox;
- ListBox2: TListBox;
- BitBtn1: TBitBtn;
- BitBtn2: TBitBtn;
- procedure ListBox2DragOver(Sender, Source: TObject; X, Y: Integer;
- State: TDragState; var Accept: Boolean);
- procedure ListBox2DragDrop(Sender, Source: TObject; X, Y: Integer);
- procedure ListBox2MouseDown(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- procedure ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
- State: TDragState; var Accept: Boolean);
- procedure FormCreate(Sender: TObject);
- procedure BitBtn1Click(Sender: TObject);
- procedure BitBtn2Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
- MoveSelectedItem : Integer; {the item in ListBox2 being moved}
- DnListBox1 : Boolean; {indicates which listbox to work with}
- DnListBox2 : Boolean; {indicates which listbox to work with}
-
- implementation
-
- {$R *.DFM}
-
- procedure TForm1.ListBox2DragOver(Sender, Source: TObject; X, Y: Integer;
- State: TDragState; var Accept: Boolean);
- begin
- if (Source is TListBox) then Accept := True;
- {because this is such a small program, 'ACCEPT := True' would work. But
- larger programs need a little more control.}
- end;
-
- procedure TForm1.ListBox2DragDrop(Sender, Source: TObject; X, Y: Integer);
- var
- i : Integer; {serves two purposes: 1) a counting variable for ListBox1,
- and 2) the item that the SELECTED item is being dropped on to
- in ListBox 2}
- begin {procedure}
- {instructions for moving items from ListBox1 to ListBox2}
- if DnListBox1 then
- begin {if 1}
- for i := 0 to ListBox1.Items.Count - 1 do {look at ALL items in ListBox1}
- begin {for}
- if ListBox1.Selected[i] then
- ListBox2.Items.Insert(ListBox2.ItemAtPos(Point(X,Y), True)
- ,ListBox1.Items[i]);
- ListBox1.Selected[i] := False; {after copying to LB2, UNselect it}
- end; {for}
- DnListBox1 := False;
- end; {if 1}
-
- {instructions for moving an item WITHIN ListBox2}
- if DnListBox2 then
- begin {if 2}
- {i = the item UNDER the moving, selected item}
- i := ListBox2.ItemAtPos(Point(X, Y), True);
- ListBox2.Items.Move(MoveSelectedItem, i); {puts the moved item into place}
- ListBox2.ItemIndex := i; {select (highlight) the item you moved}
- if i = -1 then ListBox2.ItemIndex := ListBox2.Items.Count-1;
- DnListBox2 := False;
- end; {if 2}
- end; {procedure}
-
- procedure TForm1.ListBox2MouseDown(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- begin {procedure}
- DnListBox1 := False;{tells the OnDragDrop procedure which instructions to use}
- DnListBox2 := True;{tells the OnDragDrop procedure which instructions to use}
- if Button = mbLeft then
- if ListBox2.ItemAtPos(Point(X, Y), True) >= 0 then
- MoveSelectedItem := ListBox2.ItemIndex;
- end; {procedure}
-
- procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
- State: TDragState; var Accept: Boolean);
- begin
- DnListBox1 := True;
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- {I just threw these in here to look nice. They can be pretty handy.}
- SendMessage(ListBox1.Handle, LB_SetHorizontalExtent, 1000, LongInt(0));
- SendMessage(ListBox2.Handle, LB_SetHorizontalExtent, 1000, LongInt(0));
- end;
-
- procedure TForm1.BitBtn1Click(Sender: TObject);
- var
- i : Integer;
- begin {procedure}
- if ListBox2.ItemIndex > 0 then
- begin {if}
- i := ListBox2.ItemIndex;
- ListBox2.Items.Move(i, i-1);
- ListBox2.ItemIndex := i-1;
- end; {if}
- end; {procedure}
-
- procedure TForm1.BitBtn2Click(Sender: TObject);
- var
- i : Integer;
- begin {procedure}
- if (ListBox2.ItemIndex < ListBox2.Items.Count-1) and
- (ListBox2.ItemIndex <> -1) then
- begin {if}
- i := ListBox2.ItemIndex;
- ListBox2.Items.Move(i, i+1);
- ListBox2.ItemIndex := i+1;
- end; {if}
- end; {procedure}
-
- end.
-